/*
 * Copyright (c) 2008 Kevin Wetzels
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package be.roam.jindy.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/**
 * Utility methods for dealing with input and output.
 * 
 * @author Kevin Wetzels
 * 
 */
public class IOUtils {

	/**
	 * Close the stream, ignoring any exceptions that might be thrown.
	 * 
	 * @param stream
	 *            stream to close
	 */
	public static void close(InputStream stream) {
		if (stream == null) {
			return;
		}
		try {
			stream.close();
		} catch (IOException e) {
			// Do nothing
		}
	}

	/**
	 * Close the stream, ignoring any exceptions that might be thrown.
	 * 
	 * @param stream
	 *            stream to close
	 */
	public static void close(OutputStream stream) {
		if (stream == null) {
			return;
		}
		try {
			stream.close();
		} catch (IOException e) {
			// Do nothing
		}
	}

	/**
	 * Close the reader, ignoring any exceptions that might be thrown.
	 * 
	 * @param reader
	 *            reader to close
	 */
	public static void close(Reader reader) {
		if (reader == null) {
			return;
		}
		try {
			reader.close();
		} catch (IOException e) {
			// Do nothing
		}
	}

	/**
	 * Close the writer, ignoring any exceptions that might be thrown.
	 * 
	 * @param writer
	 *            writer to close
	 */
	public static void close(Writer writer) {
		if (writer == null) {
			return;
		}
		try {
			writer.close();
		} catch (IOException e) {
			// Do nothing
		}
	}

}
